package com.huifai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huifai.entity.SysOrg;
import com.huifai.mapper.SysOrgMapper;
import com.huifai.service.ISysOrgService;
import com.huifai.vo.request.SysOrgRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author wcj
 * @since 2019-07-16
 */
@Service
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements ISysOrgService {
    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Override
    public List<SysOrg> findSysOrgByName(String orgName) {
        QueryWrapper<SysOrg> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("org_name",orgName);
        return sysOrgMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysOrg> getChildrenOrgById(Long orgId) {
        QueryWrapper<SysOrg> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("org_pid",orgId);
        return sysOrgMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<SysOrg> pageQuery(SysOrgRequest sysOrgRequest, Page<SysOrg> page) {
        QueryWrapper<SysOrg> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysOrgRequest.getOrgName())){
            queryWrapper.like("org_name",sysOrgRequest.getOrgName());
        }
        if(StringUtils.isNotBlank(sysOrgRequest.getOrgCode())){
            queryWrapper.like("org_code",sysOrgRequest.getOrgCode());
        }
        if(sysOrgRequest.getOrgType()!=null){
            queryWrapper.eq("org_type",sysOrgRequest.getOrgType());
        }
        queryWrapper.orderByDesc("create_time");
        return sysOrgMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<SysOrg> listOrgsByIds(List<Long> orgIdList) {
        QueryWrapper<SysOrg> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("id",orgIdList);
        return this.list(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> listOrgTree(List<SysOrg> orgList) {
        List<Map<String,Object>> orgResult=new ArrayList<>();
        List<SysOrg> firstOrgList = new ArrayList<>();
        List<SysOrg> childOrgList = new ArrayList<>();
        if(orgList!=null && orgList.size()>0){
            for (SysOrg sysOrg : orgList) {
                if(sysOrg.getOrgPid()==0){
                    firstOrgList.add(sysOrg);
                }else {
                    childOrgList.add(sysOrg);
                }
            }
            for (SysOrg sysOrg : firstOrgList) {
                Map<String,Object> orgMap = new LinkedHashMap<>();
                orgMap.put("id",sysOrg.getId());
                orgMap.put("label",sysOrg.getOrgName());
                orgMap.put("children",getOrgTree(childOrgList,sysOrg.getId()));
                orgResult.add(orgMap);
            }
        }
        return orgResult;
    }

    /**
     * 递归取出下级所有关系树
     * @param list
     * @param pid
     * @return
     */
    private List<Map<String,Object>> getOrgTree(List<SysOrg> list, Long pid) {
        List<Map<String,Object>> sysOrgList=new ArrayList<>();
        //取出所有菜单
        for (int i = 0;i< list.size();i++) {
            SysOrg sysOrg = list.get(i);
            if (sysOrg.getOrgPid() == pid ) {
                Map<String,Object> orgMap = new LinkedHashMap<>();
                orgMap.put("id",sysOrg.getId());
                orgMap.put("label",sysOrg.getOrgName());
                orgMap.put("children",getOrgTree(list,sysOrg.getId()));
                sysOrgList.add(orgMap);
            }
        }
        return sysOrgList;
    }
}
